package com.xdf.wxbebarrel.service;

import com.xdf.wxbebarrel.model.User;
import com.xdf.wxbebarrel.model.UserMill;
import com.xdf.wxbebarrel.model.WechatUserInfo;
import com.xdf.wxbebarrel.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 对微信操作的响应：关注、取消关注、点击事件等等
 */
@Service
public class WechatOpe {

    @Autowired
    private ReqsLogService dbLog;
    @Autowired
    private UserService userService;

    @Autowired
    private UserMillService userMillService;

    // 订阅
    public Boolean subscribe(WechatUserInfo userInfo){
        if( userInfo == null ) throw new BusinessException("执行订阅逻辑时传入了空对象！");
        // 保存用户表
        User user = userService.selectByOpenId(userInfo.getOpenid());
        if( user != null ) return true;// 已经关注过就不再追加了
        user = new User();
        user.setOpenId(userInfo.getOpenid());
        user.setNickName(userInfo.getNickname());
        userService.save(user);
        return true;
    }

    /**
     * 为新用户在表 user 、user_mill 写入数据
     * @param userInfo
     * @param mill
     * @return
     */
    public Boolean subscribe(WechatUserInfo userInfo,String mill){
        if( userInfo == null ) throw new BusinessException("执行订阅逻辑时传入了空对象！");
        mill = Optional.ofNullable(mill).orElse("");
        if( mill.length()<=0 ) throw new BusinessException("传入了空mill！");

        String openid = Optional.ofNullable(userInfo.getOpenid()).orElse("");
        String nickName = Optional.ofNullable(userInfo.getNickname()).orElse("");


        // 保存用户表
        User user = userService.selectByOpenId(userInfo.getOpenid());
        if( user == null ) {// 是新用户的话才新增
            user = new User();
            user.setOpenId(openid);
            user.setNickName(nickName);
            userService.save(user);
        }

        // 写入表 user_mill
//        UserMill userMill = Optional.ofNullable(userMillService.selectUserMill(openid,mill)).orElse(new UserMill());
//        userMill.setOpen_id(openid);
//        userMill.setMill_sid(mill);
//        userMill.setEffective(true);
//        userMillService.save(userMill);


        // 2021年7月26日 17:14:38 chanchaw
        // 为防止出现一个微信用户有多张账套的问题，先删除再追加
        int delCount = userMillService.deleteByOpenid(openid);
        UserMill userMill = new UserMill();
        userMill.setOpenId(openid);
        userMill.setMillSid(mill);
        userMill.setEffective(true);
        userMillService.save(userMill);
        dbLog.insert(userMill.toString(),"新增微信mill关系");
        return true;
    }

    public Boolean setUserMill(String openId,String realName,String mill){
        User user = userService.selectByOpenId(openId);
        if( user == null ) throw new BusinessException("没有找到open=" + openId + "的用户！");
        // 保存真实姓名
        user.setRealName(realName);
        userService.save(user);

        // 保存和工厂的关系
        UserMill userMill = userMillService.selectUserMill(openId, mill);
        if( userMill == null ) userMill = new UserMill();
        userMill.setOpenId(openId);
        userMill.setMillSid(mill);
        userMillService.save(userMill);
        return true;
    }
}
